class EventBus {
	events: Map<string, Array<(data: any) => void>>;
	constructor() {
		this.events = new Map();
	}
	on(eventName: string, callback: (data: any) => void) {
		if (!this.events.has(eventName)) {
			this.events.set(eventName, []);
		}
		this.events.get(eventName)?.push(callback);
	}
	emit(eventName: string, data: any) {
		const handlers = this.events.get(eventName);
		if (handlers) {
			handlers?.slice()?.forEach((callback) => callback(data));
		}
	}
	off(eventName: string, callback?: (data: any) => void) {
		const handlers = this.events.get(eventName);
		if (handlers) {
			if (callback) {
				handlers?.splice(handlers.indexOf(callback) >>> 0, 1);
			} else {
				this.events.set(eventName, []);
			}
		}
	}
}

export default EventBus;
